package org.apache.android.media;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	OrderAdapter customAdapter;
	ArrayList<Order> orderList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainactivity);
		orderList = new ArrayList<Order>();
		orderList.add(new Order("Incremental Progress Bar"));
		orderList.add(new Order("loading progress icon"));
		orderList.add(new Order("Internet Connectivity"));
		orderList.add(new Order("File Browser"));
		customAdapter = new OrderAdapter(this, R.layout.row, orderList);

		ListView l = (ListView) findViewById(R.id.listView1);

		l.setAdapter(customAdapter);

		l.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				startActivity(getIntent(arg2));
			}
		});
	}

	Intent getIntent(int pos) {
		Intent i = new Intent();
		if (pos == 0) {
			i.setClass(this, ProgressSampleActivity.class);
		} else if (pos == 1) {
			i.setClass(this, LoadingSampleActivity.class);
		} else if (pos == 2) {
			i.setClass(this, InternetConnectivityActivity.class);
		} else if (pos == 3) {
			i.setClass(this, FileDialog.class);
		}

		return i;

	}

	private class OrderAdapter extends ArrayAdapter<Order> {
		private ArrayList<Order> items;

		public OrderAdapter(Context context, int resource,
				ArrayList<Order> items) {
			super(context, resource, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.row, null);
			}
			Order o = items.get(position);
			if (o != null) {
				TextView tt = (TextView) v.findViewById(R.id.TextView01);
				if (tt != null) {
					tt.setText("Name: " + o.getTitleName());
				}
			}
			return v;
		}
	}

	public class Order {

		public Order(String titleName) {
			this.titleName = titleName;
		}

		private String titleName;

		public String getTitleName() {
			return titleName;
		}

		public void setTitleName(String titleName) {
			this.titleName = titleName;
		}

	}

}
